Executes one of several groups of statements, depending on the value of an expression.
Notes
Syntax
Select Case testExpression
[Case [Is] expression-n
statements-n]
[Else or Case Else
elseStatements]
End [Select]
Part | Description |
|---|---|
testExpression |
Any expression that evaluates to a value. The expression can be of any data type or an object. |
expression-n |
Any expression or list of expressions. You can use a function that evaluates to the data type of testExpression. |
statements-n |
Statements to be executed if expression-n is true. |
elseStatements |
Statements to be executed if no expressions are True. |
The Select Case statement is useful when there are several possible conditions that must be checked. Unlike an If statement, the Select Case statement will exit as soon as it finds a matching Case expression and executes any statements that follow the Case expression up to the next Case expression. If there are no Case expressions that match, the elseStatements are executed.
The expression Case Else can be used as a synonym for Else.
The Case statement can accept several types of expressions. The expression can be a single value, a comma-delimited list of values, a function that returns a value, a range of values specified with the 'To" keyword, or an expression that uses the "Is" keyword to do an equality or inequality test. You can combine types of expressions, separating them by commas
Here are some examples:
Case 2 To 5 //range of values using To
Case 2 To 5, 7,9,11 //Both separate values and range
Case myFunction(x) // a Function
Case Is >= 42 // greater than/equal to operator
Case Is <19 //less than operator
Example
This example uses the Select Case statement to determine which day of the week the user has entered into an EditField and displays a message based on that information.
Dim msg As String
dayNumber= Val(EditField1.text)
Select Case dayNumber
Case 2
msg="It's Monday"
Case 3
msg="It's Tuesday"
Case 4
msg="It's Wednesday"
Case 5
msg="It's Thursday"
Case 6
msg="It's Friday"
Else
msg="It's the weekend."
End Select
MsgBox msg
The following example uses the Select Case statement to determine which button was pressed in a MessageDialog. Notice that it compares objects of type MessageDialogButton.
Dim b as MessageDialogButton //for handling the result
d.icon= MessageDialog.GraphicCaution //display warning icon
d.ActionButton.Caption="Save"
d.CancelButton.Visible= True //show the Cancel button
d.CancelButton.Cancel= True//esc key works for Cancel
d.AlternateActionButton.Visible= True //show the "Don't Save" button
d.Message="Save changes before closing?"
d.Explanation="If you don't save your changes, you will lose " _
+"all that important work you did since your last coffee break."
b=d.ShowModal //display the dialog
Select Case b //b is a MessageDialogButton
Case d.ActionButton //determine which button was pressed.
//user pressed Save
Case d.AlternateActionButton
//user pressed Don't Save
Case d.CancelButton
//user pressed Cancel
End Select
See Also
If...Then...Else statement.